<?php

namespace app\controller\property\contract;

use think\App;
use app\controller\property\Base;
use app\validate\contract\ContractBillValidate;
use app\common\repositories\bill\BillCateRepository;
use app\common\repositories\contract\ContractBillRepository;
use app\common\repositories\contract\ContractBasicRepository;


class ContractBill extends Base
{
    protected $repository;

    public function __construct(App $app, ContractBillRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyContractBillAdd'),
            'editAuth' => property_auth('propertyContractBillEdit'),
            'delAuth' => property_auth('propertyContractBillDel'),
        ]);
    }

    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'contract_id' => '',
                'property_id' => '',
                'keywords' => '',
            ]);
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        }
        return $this->fetch('contract/bill/list');
    }

    public function add()
    {
        if ($this->request->isAjax() && $this->request->get('get_type') == 'getBillCateData') {
            $where = [
                'is_show' => 1,
                'is_class' => [3],
            ];
            /** @var BillCateRepository $cateRepository */
            $cateRepository = app()->make(BillCateRepository::class);
            $cateNameData = $cateRepository->getCascaderData($this->request->propertyId, $where);
            return json()->data(['code' => 0, 'data' => $cateNameData]);
        } else if ($this->request->isPost()) {
            $param = $this->request->param([
                'contract_id' => '',
                'pay_type' => '',
                'cate_id' => '',
                'amount' => '',
                'remark' => '',
            ]);
            ## 验证 同一费用类别同份合同只能添加一次
            $countNum = $this->repository->getSearch([])
                ->where('property_id', $this->request->propertyId)
                ->where('contract_id', $param['contract_id'])
                ->where('cate_id', $param['cate_id'])
                ->where('status', 1)
                ->count();
            if ($countNum > 0) {
                return $this->error('当前费用类目己存在');
            }
            $param['if_del'] = 2; ## 自定义添加的费用允许删除
            $param['if_bill'] = 3;
            validate(ContractBillValidate::class)->scene('add')->check($param);
            try {
                $res = $this->repository->addInfo($this->request->propertyId, $param, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加租赁账单失败', $e);
                return $this->error('网络失败');
            }
        } else {
            return $this->fetch('contract/bill/add', [
                'payTypeData' => $this->repository::PAY_TYPE
            ]);
        }

    }

    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'pay_type' => '',
                'amount' => '',
                'remark' => '',
            ]);
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('修改租赁账单失败', $e);
                return $this->error('网络错误');
            }
        } else {
            if ($info['if_bill'] == 1) {
                ## 押金
                $payTypeData = [
                    4 => '一次性支付',
                ];
            } else if ($info['if_bill'] == 2) {
                ## 租金
                $payTypeData = [
                    1 => '按月份支付',
//                    2 => '按季度支付',
//                    3 => '按年度支付',
//                    4 => '一次性支付',
                ];
            } else {
                ## 其它费用
                $payTypeData = [
                    4 => '一次性支付',
                    5 => '随租金支付',
                ];
            }
            return $this->fetch('contract/bill/edit', [
                'info' => $info,
                'payTypeData' => $payTypeData
            ]);
        }
    }

    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除租赁账单失败', $e);
            return $this->error('网络失败');
        }
    }

}